package com.matt.domain.statistics;

import com.matt.foundation.model.Domain;
import com.matt.foundation.utils.DateUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Date;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/7/18
 * 实际收入统计
 * *******************************
 */
@Data
@Entity
@Table(name = "stat_paid_contract")
@EqualsAndHashCode(callSuper = true)
public class StatPaidContract extends Domain {

    @Column(columnDefinition = "varchar(32)  COMMENT '对应数'")
    private Double count;

    @Column(columnDefinition = "varchar(32)  COMMENT '对应产品名称'")
    private String name;

    @Column(columnDefinition = "decimal(12, 2) COMMENT '总金额'")
    private BigDecimal totalAmount;

    @Column(columnDefinition = "varchar(32) COMMENT '产品id 0:全部, 对应产品id'")
    private String productId;

    @Column(columnDefinition = "decimal(12, 2) COMMENT '客单价'")
    private BigDecimal unitPrice;

    @Column(columnDefinition = "varchar(32) COMMENT '对应园区 id'")
    private String areaId;

    public static StatPaidContract of(String productId, String productName, String areaId, Date date) {
        StatPaidContract contract = new StatPaidContract();
        contract.setCount(0d);
        contract.setTotalAmount(BigDecimal.ZERO);
        contract.setUnitPrice(BigDecimal.ZERO);
        contract.setName(productName);
        contract.setAreaId(areaId);
        contract.setProductId(productId);
        contract.setCreated(date);
        return contract;
    }

    public StatPaidContract refreshUnitPrice() {
        this.setUnitPrice(this.getCount() == 0 ? BigDecimal.ZERO : this.getTotalAmount().divide(BigDecimal.valueOf(this.getCount()), RoundingMode.HALF_UP));
        return this;
    }
}
